<template>
	<view>
		<button class="button-selectCover" @tap="chooseCover">
			<image class="image-selectCover" :src="coverUrl" />
		</button>
		<button class="button-publish" @tap="uploadArticle">发布</button>
	</view>
</template>

<script>
	const defaultCoverUrl = '/static/selectCover.png'
	export default {
		data() {
			return {
				coverUrl: defaultCoverUrl,
				html: '',
				title: '',
				count: 1
			}
		},
		onLoad: function(options) {
			this.html = decodeURIComponent(options.html)
			this.title = decodeURIComponent(options.title)
		},
		methods: {
			uploadArticle() {
				const that = this
				var d = new Date();
				var hours = d.getHours()
				var minutes = d.getMinutes()
				var timee = hours + ":" + minutes
				wx.cloud.database().collection('article').add({
					data: {
						author: getApp().globalData.userdata.nickname,
						avatar: getApp().globalData.userdata.avatar,
						comment: 0,
						content: that.html,
						cover: that.coverUrl,
						date: that.getDate(new Date(), 'mm-dd'),
						time: timee,
						title: that.title,
						view: 0
					},
					success: () => {
						uni.switchTab({
							url: "/pages/info/info"
						})
					}
				})
			},
			chooseCover() {
				const that = this
				uni.chooseImage({
					count: that.count, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册里面选择
					success: function(res) {
						console.log(res.tempFilePaths[0])
						var uuid = new Date().getTime() + Math.random().toString(36).substr(2);
						// 图片数据
						const imageDataURI = res.tempFilePaths[0];
						// 获取最后一个"."出现的位置，将图片的后缀截取下来
						const dotIndex = imageDataURI.lastIndexOf('.');
						// 使用substring()函数从dotIndex开始截取到字符串末尾
						const extension = imageDataURI.substring(dotIndex);
						// 将图片上传至云存储空间
						wx.cloud.uploadFile({
							// 指定上传到的云路径
							cloudPath: 'cover/' + uuid + extension,
							// 指定要上传的文件的小程序临时文件路径
							filePath: imageDataURI,
							// 成功回调
							success: res => {
								console.log('上传成功', res)
								const fileId = res.fileID
								// 获取图片URL(imgUrl)
								wx.cloud.getTempFileURL({
									fileList: [fileId],
									success: res => {
										console.log('获取URL成功', res
											.fileList[0]
											.tempFileURL)
										const imgUrl = res.fileList[0]
											.tempFileURL
										console.log("上传完成了" + imgUrl)
										console.log(that.editorCtx)
										console.log("editorCtx")
										that.coverUrl = imgUrl
									},
									fail: console.error
								})
							},
						})
					}
				})
			},
			getDate(date, format) {
				const map = {
					mm: date.getMonth() + 1,
					dd: date.getDate(),
					yy: date.getFullYear().toString().slice(-2),
					yyyy: date.getFullYear()
				}

				return format.replace(/mm|dd|yy|yyy/gi, matched => map[matched])
			}
		}
	}
</script>

<style>
	.button-selectCover {
		width: 500rpx;
		height: 500rpx;
		margin-top: 100rpx;
	}

	.image-selectCover {
		width: 500rpx;
		height: 500rpx;
	}
	
	.button-publish {
		position: fixed;
		width: 100%;
		bottom: 24rpx;
		padding: 0;
	}
</style>
